<template>
  <div class="box-show">
    <div class="z gonggao">
      <el-page-header
        style="color: #999999"
        @back="$router.back(-1)"
        content="修改门店信息"
      >
      </el-page-header>
      <div style="height: 20px"></div>
    </div>
    <div class="shuju">
      <el-row>
        <el-col :span="8">
          <el-form ref="form" :model="form" label-width="80px" size="mini">
            <el-form-item
              label="门店名称"
              prop="name"
              :rules="[{ required: true, message: '门店名称不能为空' }]"
            >
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item
              label="仓库代码"
              prop="code"
              :rules="[{ required: true, message: '门店代码不能为空' }]"
            >
              <el-input v-model="form.code" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="省市区" prop="couponName">
              <el-col :span="18">
                <el-cascader
                  :options="options"
                  v-model="selectedOptions"
                  @change="handleChange"
                  style="width: 100%"
                >
                </el-cascader>
              </el-col>
            </el-form-item>
            <el-form-item label="详细位置">
              <el-input v-model="form.address"></el-input>
            </el-form-item>
            <el-form-item label="门店简介">
              <el-input type="textarea" v-model="form.intro"></el-input>
            </el-form-item>

            <el-form-item style="text-align: left">
              <el-button type="primary" @click="onSubmit('form')"
                >立即创建</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { storehouse_save, storehouse_getInfoById } from "@/utils/do";
// 省市区
import { regionData, CodeToText, TextToCode } from "element-china-area-data";
export default {
  name: "add",
  props: {},
  data() {
    return {
      form: {
        id: this.$route.params.id,
        type: 2,
        name: "",
        code: "",
        intro: "",
        position: "", //位置信息
        address: "" // 详细地址
      },
      options: regionData,
      selectedOptions: [], // 省市区代码数组
      loading: true
    };
  },
  components: {
    // 第三方组件实例化
  },
  computed: {},
  methods: {
    // 方法
    onSubmit(form) {
      this.$store.commit("setIsLoading", true);
      let _this = this;
      this.$refs[form].validate(valid => {
        if (valid) {
          console.log("提交参数", _this.form);
          // return;
          storehouse_save(_this.form, res => {
            _this.$store.commit("setIsLoading", false);
            console.log(res);
            if (res.code != 0) {
              _this.$message.error(res.msg);
              return;
            }
            _this.$alert("门店信息修改成功", "提醒", {
              confirmButtonText: "确定",
              callback: action => {
                console.log("点击了=", action);
                _this.$router.push("/Storehouse/ShppingList/List");
              }
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleChange(value) {
      // console.log("111", value);
      // console.log("222", CodeToText["110000"]);
      console.log("222", TextToCode["北京市"]);
      this.form.position =
        CodeToText[value[0]] +
        "-" +
        CodeToText[value[1]] +
        "-" +
        CodeToText[value[2]];
    },
    storehouse_getInfoById() {
      this.$store.commit("setIsLoading", true);
      let _this = this;
      storehouse_getInfoById(_this.form.id, res => {
        this.$store.commit("setIsLoading", false);
        console.log("门店详细信息", res);
        if (res.code == 0) {
          _this.form = res.date;
          var stringResult = res.date.position.split("-");

          var v1 = TextToCode[stringResult[0]].code;
          var v2 = TextToCode[stringResult[0]][stringResult[1]].code;
          var v3 =
            TextToCode[stringResult[0]][stringResult[1]][stringResult[2]].code;
          _this.selectedOptions = [v1, v2, v3];
          return;
        }
        _this.$message.error(res.msg);
      });
    }
  },
  mounted() {
    this.storehouse_getInfoById();
  }
};
</script>

<style scoped>
@import "~@/assets/scss/index.css";
</style>
